
package com.gamutsoft.amb.common.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;

import com.gamutsoft.amb.common.bean.CondictionsBean;
import com.gamutsoft.amb.common.bean.QueryExample;

public class ConditionUtil
{
	/**
	 * 查询条件自动复制，用于Hibernate查询
	 * 
	 * @author 
	 * @param request
	 * @param entityClass
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public static QueryExample copyCondition(HttpServletRequest request ,Class entityClass) throws SecurityException, NoSuchFieldException{
		QueryExample qe = new QueryExample();
		Map parameterMap = request.getParameterMap();
		Field[] fields = entityClass.getFields();
		if(fields != null && fields.length > 0){
			for(Field field : fields){
				String name = field.getName();
				Class typeClass = field.getType();
				CondictionsBean bean = null;
				if(parameterMap.get( "condition_"+name ) != null){
					bean = copyTypeValue(typeClass , parameterMap.get( "condition_" + name ) , "=");
					qe.addCondictions( bean );
				}else if(parameterMap.get( "like_" + name ) != null){
					bean = copyTypeValue(typeClass , parameterMap.get( "condition_" + name ) , "like");
					qe.addCondictions( bean );
				}else if(parameterMap.get( "bet_" + name + "_start" ) != null){
					//TODO 时间间隔条件查询
					
				}
			}
		}
		return qe;
	}
	
	/**
	 * 查询规则自动复制,用于SQL查询
	 * 普通查询条件前面加condition_ + 字段名
	 * 	1、如是int型在后面加 _int
	 * 	2、如是date型在后面加 _date
	 *  3、String型后面不加任何字符
	 *  
	 * 模糊匹配查询前面加 like_ + 字段名 ， 暂时模糊匹配规则只能是字符串
	 * 
	 * 日期间隔查询前面加 bet_ + 字段名，开始日期后面加 _start ,结束日期加 _end
	 * 
	 * @author Vincent.luo
	 * @param request
	 * @return
	 */
	public static QueryExample copyConditionBySql(HttpServletRequest request){
		QueryExample qe = new QueryExample();
		Map parameterMap = request.getParameterMap();
		Map conditionMap = new HashMap();
		if(parameterMap != null && parameterMap.size() > 0){
			for(Object obj : parameterMap.keySet()){
				String name = String.valueOf( obj );
				//Object value = parameterMap.get( name );
				String value = request.getParameter( name );
				try {
					value=java.net.URLDecoder.decode(value , "UTF-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 

				
				//过滤sql中非法字符
				//value = SqlQueryFilter.filterSql(value);
				
				if(value != null && !"".equals( value)){
					if(name.startsWith( "condition_" )){
						CondictionsBean condition = new CondictionsBean();
						condition.setCondiction( "=" );
						if(name.endsWith( "_int" )){
							condition.setProperty( name.substring( name.indexOf( "condition_" )+10 , name.indexOf( "_int" ) ) );
							condition.setValue( Integer.parseInt( value ) );
						}else if(name.endsWith( "_date" )){
							condition.setProperty( name.substring( name.indexOf( "condition_" )+10 , name.indexOf( "_date" ) ) );
							condition.setValue( DateUtil.parse( value, "yyyy-MM-dd" ) );
						}else{
							condition.setProperty( name.substring( name.lastIndexOf( "condition_" )+10 ) );
							condition.setValue( value );
						}
						qe.addCondictions( condition );
						conditionMap.put( name, value );
					}else if(name.startsWith( "bet_" )){
						if(name.endsWith( "_start" )){
							String propertyName = name.substring( name.indexOf( "bet_" )+4, name.indexOf( "_start" ) );
							Map<String , Date> timeMap = qe.getTimeMap( propertyName );
							if(timeMap == null){
								timeMap = new HashMap<String , Date>();
							}
							timeMap.put( "start", DateUtil.parse( value.toString(), "yyyy-MM-dd" ) );
							qe.addTimeCondictions( propertyName, timeMap );
						}else if(name.endsWith( "_end" )){
							String propertyName = name.substring( name.indexOf( "bet_" )+4, name.indexOf( "_end" ) );
							Map<String , Date> timeMap = qe.getTimeMap( propertyName );
							if(timeMap == null){
								timeMap = new HashMap<String , Date>();
							}
							timeMap.put( "end", DateUtil.parse( value.toString(), "yyyy-MM-dd" ) );
							qe.addTimeCondictions( propertyName, timeMap );
						}
						conditionMap.put( name, value );
					}else if(name.startsWith( "like_" )){
						CondictionsBean condition = new CondictionsBean();
						condition.setCondiction( "like" );
						String propertyName = name.substring( name.indexOf( "like_" )+5 );
						condition.setProperty( propertyName );
						condition.setValue( "%" + StringEscapeUtils.escapeSql(value) + "%" );
						qe.addCondictions( condition );
						conditionMap.put( name, value );
					}
				}
			}
			request.setAttribute( "conditionMap", conditionMap );
		}
		//qe.setEcPageUtil( new EcPageUtil(request) );
		return qe;
	}
	
	public static CondictionsBean copyTypeValue(Class typeClass , Object value , String condiction){
		CondictionsBean bean = new CondictionsBean();
		bean.setCondiction( condiction );
		bean.setProperty( typeClass.getName() );
		if(Integer.class.equals( typeClass )){
			bean.setValue( Integer.parseInt( value.toString() ) );
		}else if(String.class.equals( typeClass )){
			bean.setValue( value.toString() );
		}else if(Date.class.equals( typeClass )){
			DateUtil.parse( value.toString(), "YYYY-MM-DD" );
		}
		return bean;
	}
}

